package com.skyline.base.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.skyline.base.dao.NoticeDao;
import com.skyline.base.mapper.NotificationMapper;
import com.skyline.base.model.Notification;
import com.skyline.base.type.NotificationType;
import com.skyline.common.cache.annotation.Cache;
import com.skyline.common.cache.annotation.CacheCategoryType;
import com.skyline.common.cache.annotation.CacheDelete;
import com.skyline.common.cache.annotation.Fk;
import com.skyline.common.cache.annotation.Param;

/**
 * NoticeDaoImpl为NoticeDao的实现，即实现notice的功能
 * 
 * @author jairus
 * 
 * @version 0.1
 */
@Repository("noticeDao")
public class NoticeDaoImpl extends BaseDaoImpl implements NoticeDao {

	@Value("${NoticeDao.insertNotication}")
	private String insertNoticationSql;

	@Value("${NoticeDao.findNotificationByOwnerId}")
	private String findNotificationByOwnerIdSql;

	@Value("${NoticeDao.findNotificationById}")
	private String findNotificationByIdSql;

	//@Value("${NoticeDao.deleteNotificationById}")
	//private String deleteNotificationByIdSql;

	@Value("${NoticeDao.deleteNotificationByResource}")
	private String deleteNotificationByResourceSql;

	Log log = LogFactory.getLog(NoticeDaoImpl.class);

	@Override
	@CacheDelete(type = CacheCategoryType.NOTIFICATION)
	public void insertNotication(@Fk Long ownerId, Long notifierId, String notifierNickname, Long resourceId,
			String resourceTitle, NotificationType type) {
		//log.debug(insertNoticationSql + "," + ownerId + "," + notifierId + "," + notifierNickname + "," + resourceId
			//	+ "," + resourceTitle + "," + type.toString());
		jdbcTemplate.update(insertNoticationSql, ownerId, notifierId, notifierNickname, resourceId, resourceTitle,
				type.toString());
	}

	@Override
	@Cache(keyPattern = "notifcation-:uid", type = CacheCategoryType.NOTIFICATION)
	public List<Notification> findNotificationByOwnerId(@Param("uid") Long ownerId) {
		return jdbcTemplate.query(findNotificationByOwnerIdSql, NotificationMapper.getMapper(), ownerId);
	}

	@Override
	public Notification findNotificationById(Long id) {
		List<Notification> notifications = jdbcTemplate.query(findNotificationByIdSql, NotificationMapper.getMapper(),
				id);
		if (notifications == null || notifications.size() <= 0) {
			return null;
		} else {
			Notification notification = notifications.get(0);
			//this.deleteNotificationByResource(notification.getOwnerId(),notification.getResourceId(), notification.getType());
			return notification;
		}
	}

//	@Override
//	public void deleteNotificationById(Long id) {
//		jdbcTemplate.update(deleteNotificationByIdSql, id);
//	}

	@Override
	@CacheDelete(type = CacheCategoryType.NOTIFICATION)
	public void deleteNotificationByResource(@Fk Long ownerId, Long resourceId, NotificationType type) {
		jdbcTemplate.update(deleteNotificationByResourceSql,  ownerId, resourceId, type.toString());
	}

}
